package com.hp.btc.controller;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import com.hp.btc.bean.BeanVDNResponse;

@Controller
@RequestMapping("/transferVDN")
public class
TransferVDNController extends BaseController {


	private static final Logger logger = LoggerFactory.getLogger(TransferVDNController.class);
	
	@RequestMapping(value="/getTransferVDN/{campaing}/{option}/{calledNumber}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody BeanVDNResponse getTransferVDN(@PathVariable("campaing") String campaing, @PathVariable("option") String option, @PathVariable("calledNumber") String calledNumber){
		
		logger.info("getTransferVDN campaing:"+campaing+",option="+option+",calledNumber="+calledNumber);
		
		Calendar currentDate = new GregorianCalendar();
		
		Integer day = currentDate.get(Calendar.DAY_OF_WEEK);
		Integer hour = currentDate.get(Calendar.HOUR_OF_DAY);
		
		if(day != currentDay || hour != currentHour) {
			loadFiles();
			currentDay = day;
			currentHour = hour;
		}
		
		BeanVDNResponse responseBean = desService.getTransferVdn(campaing,option,calledNumber);

		logger.info("BeanVDNResponseRequest|campaing="+campaing
										 +"|option="+option
										 +"|calledNumber="+calledNumber
										 +"|BeanVDNResponseResponse|vdn="+responseBean.getVdn()
										 +"|errorCode="+responseBean.getErrorCode()
										 +"|errorMessage="+responseBean.getMessage());
		
		return responseBean;
	}
	
	@RequestMapping("/monitor")
	public ModelAndView showPageMonitorInit(){
		logger.debug(" ******* start page *****");
		ModelAndView mav = new ModelAndView("monitor");
		mav.addObject("descriptionVDNMonitor", desService.getAllVDNCurrent());
		return mav;
	}
	
	@RequestMapping("/monitor/reload")
	public String reload(Model model){
		logger.debug(" ******* reload *****");

		loadFiles();
		model.addAttribute("descriptionVDNMonitor", desService.getAllVDNCurrent());
		return "monitor :: reloadVDNs";
	}
	
	@RequestMapping("/monitor/refreshCounter")
	public String refreshCounter(Model model){
		logger.debug(" ******* refresh *****");

		desService.resetCounter();
		model.addAttribute("descriptionVDNMonitor", desService.getAllVDNCurrent());
		return "monitor :: reloadVDNs";
	}
	
	@RequestMapping("/monitor/monitorReload")
	public String reloadMonitor(Model model){

		model.addAttribute("descriptionVDNMonitor", desService.getAllVDNCurrent());
		return "monitor :: reloadVDNs";
	}
}
